package algorithm.sort;

import algorithm.utils.Utils;
import org.junit.Test;

import java.util.*;

public class MaopaoSort {

  @Test
  public void test() {

    for (int k = 0; k < 10; k++) {
      int[] arr = Utils.RandomArr(4);

      System.out.println(Arrays.toString(arr));
// big small
      for (int i = 0; i < arr.length - 1; i++) {
        for (int j = 0; j < arr.length - 1 - i; j++) {
          if (arr[j] < arr[j + 1]) {
            int tmp = arr[j];
            arr[j] = arr[j + 1];
            arr[j + 1] = tmp;
          }
        }
      }

      System.out.println(Arrays.toString(arr));
      System.out.println();
    }

  }


  @Test
  public void test2() {
    List<GlobalIndex> docList = new ArrayList<>();
    docList.add(new GlobalIndex(1, 3, "A1", "a"));
    docList.add(new GlobalIndex(3, 2, "A2", "a"));
    docList.add(new GlobalIndex(6, 3, "b1", "b"));
    docList.add(new GlobalIndex(10, 1, "b3", "b"));
    docList.add(new GlobalIndex(7, 1, "A3", "a"));
    docList.add(new GlobalIndex(8, 2, "b2", "b"));

    //排序

    docList.sort((o1, o2) -> {
      if (o1.score < o2.score) return 1;
      if (o1.score > o2.score) return -1;
      return 0;
    });

    for (GlobalIndex globalIndex : docList) {
      System.out.println(globalIndex.word);
    }

    HashMap<String, List<Integer>> typeIndexMap = new HashMap<>();

    //存放指针
    for (int i = 0; i < docList.size(); i++) {
      String type = docList.get(i).type;
      List<Integer> integers = typeIndexMap.computeIfAbsent(type, s -> new ArrayList<>());
      integers.add(i);
    }

    for (Map.Entry<String, List<Integer>> typeIndexEntry : typeIndexMap.entrySet()) {
      System.out.println(typeIndexEntry.getKey() + "：" + typeIndexEntry.getValue());
      List<Integer> value = typeIndexEntry.getValue();
      //组内冒泡排序
      for (int i = 0; i < value.size() - 1; i++) {
        for (int j = 0; j < value.size() - 1 - i; j++) {
          //current
          Integer index1 = value.get(j);
          GlobalIndex globalIndex1 = docList.get(index1);
          //next
          Integer index2 = value.get(j + 1);
          GlobalIndex globalIndex2 = docList.get(index2);
          if (globalIndex1.childScore < globalIndex2.childScore) {
            docList.set(index1, globalIndex2);
            docList.set(index2, globalIndex1);
          }
        }
      }

    }

    for (GlobalIndex globalIndex : docList) {
      System.out.println(globalIndex.word);
    }

  }

  class GlobalIndex {
    private final String type;
    double score;
    double childScore;
    String word;

    public GlobalIndex(double score, double childScore, String word, String type) {
      this.score = score;
      this.childScore = childScore;
      this.word = word;
      this.type = type;
    }
  }

}
